import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;

import org.jdesktop.swingx.JXDatePicker;

import CustomSwing.JCLabel;
import CustomSwing.JCTextField;
import CustomSwing.TableColumnAdjuster;

@SuppressWarnings("serial")
public class VendorCountPanel extends JPanel {
	public String USER_FUNC = "Vendor Count";
	private Font custFont = new Font("Arial Bold", Font.BOLD, 15);
	
	private Db db;
	private String dateStart;
	private String dateEnd;

	private JXDatePicker datePickerStart = new JXDatePicker();
	private JXDatePicker datePickerEnd = new JXDatePicker();

	private String[] columnNames = { "Date", "Number of vendors"};
	private String[][] data;
	private JScrollPane tableSP;
	private JPanel tableSPPanel = new JPanel(new GridLayout(1, 3));

	public VendorCountPanel(Db theDB) {
		db = theDB;
		setDatePicker(datePickerStart, true);
		setDatePicker(datePickerEnd, false);

		Border b = BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK),
				BorderFactory.createEmptyBorder(3, 3, 3, 3));
		JPanel startDatePanel = new JPanel(new GridLayout(2, 1));
		startDatePanel.setBorder(b);
		startDatePanel.add(new JCLabel("Start Date"));
		startDatePanel.add(datePickerStart);

		JPanel endDatePanel = new JPanel(new GridLayout(2, 1));
		endDatePanel.setBorder(b);
		endDatePanel.add(new JCLabel("End Date"));
		endDatePanel.add(datePickerEnd);

		JPanel dateRangePanel = new JPanel(new GridLayout(1, 2, 5, 0));
		dateRangePanel.add(startDatePanel);
	
		dateRangePanel.add(endDatePanel);
	

		refreshPanel();

		setLayout(new BorderLayout(5, 5));
		add(dateRangePanel, BorderLayout.NORTH);
		add(tableSPPanel, BorderLayout.CENTER);

	}

	private void refreshPanel() {
		tableSPPanel.removeAll();
		getData();
		tableSPPanel.add(new JPanel());
		tableSPPanel.add(tableSP);
		tableSPPanel.add(new JPanel());
		tableSPPanel.revalidate();
	}

	private void getData() {
		ArrayList<String[]> vendorCountData = new ArrayList<String[]>();
		
		try {
			vendorCountData = db.venderCount(dateStart, dateEnd);
		} catch (ClassNotFoundException e1) {

			e1.printStackTrace();
		} catch (SQLException e1) {

			e1.printStackTrace();
		}

		data = new String[vendorCountData.size()][columnNames.length];
		for (int i = 0; i < vendorCountData.size(); i++) {
			String[] rowData = vendorCountData.get(i);
			
			
			for (int j = 0; j < rowData.length; j++) {
				if (rowData[j] == null)
					data[i][j] = "";
				else 
					data[i][j] = " "+rowData[j]+" ";
				
			}
			

		}
		 setTable();
	}

	private void setTable() {
		JTable table = new JTable(data, columnNames);
		table.setFont(custFont);
		table.setEnabled(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumnAdjuster tca = new TableColumnAdjuster(table);
		tca.adjustColumns();
		tableSP = new JScrollPane(table);
		tableSP.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK),
				BorderFactory.createEmptyBorder(3, 3, 3, 3)));
	}

	private void setDatePicker(final JXDatePicker dp, final boolean startingDate) {
		dp.setDate(Calendar.getInstance().getTime());
		dp.setFormats(new SimpleDateFormat("MM / dd / yyyy"));
		dp.getEditor().setEditable(false);
		dp.getEditor().setFont(custFont);
		dp.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
				String s = dp.getEditor().getText().trim();
				String date = s.substring(10, 14) + "-" + s.substring(0, 2)
						+ "-" + s.substring(5, 7);
				if (startingDate)
					dateStart = date;
				else
					dateEnd = date;
				refreshPanel();

				if (datePickerStart.getDate().after(datePickerEnd.getDate())) {
					JOptionPane.showConfirmDialog(null,
							"Start date is after the end date!", "Warning",
							JOptionPane.CLOSED_OPTION);
				}
			}
		});
		String s = dp.getEditor().getText().trim();
		String date = s.substring(10, 14) + "-" + s.substring(0, 2) + "-"
				+ s.substring(5, 7);
		dateStart = dateEnd = date;
	}
}
